package com.lhgg.sd;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName SnapshotList
 * @Description TODO 所有快照的集合
 * @Author 黄鹏祥
 * @Date 2020/12/10 21:40
 * @Version 1.0
 **/
public class SnapshotList {

    private static class SingletonHolder {
        private static SnapshotList instance = new SnapshotList();
    }

    public static SnapshotList getInstance() {
        return SingletonHolder.instance;
    }

    //    所有快照的集合
    private final List<Snapshot> SNAPSHOT_LIST = new LinkedList<>();

    /**
     * 加入快照
     *
     * @param snapshot
     */
    public void put(Snapshot snapshot) {
        this.SNAPSHOT_LIST.add(snapshot);
    }

    /**
     * 移除最后一个快照，并返回快照集合的剩余个数
     */
    public int removeLast() {
        final int size = this.SNAPSHOT_LIST.size();
        if (size > 0) {
            this.SNAPSHOT_LIST.remove(size - 1);
        }
        return size - 1;
    }

    /**
     * 获取最后一个快照
     *
     * @return
     */
    public Snapshot getLast() {
        final int size = this.SNAPSHOT_LIST.size();
        if (size > 0) {
            return this.SNAPSHOT_LIST.get(size - 1);
        }
        return null;
    }

    /**
     * 清空快照
     */
    public void clear() {
        this.SNAPSHOT_LIST.clear();
    }

}